package com.example.lopngoaingu;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

	private Button btnProcess;
	private TextView tvMessage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tvMessage = (TextView) findViewById(R.id.tvMessage);
		btnProcess = (Button) findViewById(R.id.btnProcess);

		btnProcess.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				new LoadImageConversation().execute();
			}
		});
	}

	class LoadImageConversation extends AsyncTask<Void, Void, Void> {
		ProgressDialog pDialog;

		@Override
		protected void onPreExecute() {
			pDialog = new ProgressDialog(MainActivity.this);
			pDialog.setMessage("Loading");
			pDialog.show();
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Void... params) {
			for (int i = 1; i <= 60; i++) {
				/*LoadImage(
						"http://lopngoaingu.com/listening/streamline-english/Directions/img/"
								+ i + "a.jpeg", i, "ESA4","a");
				LoadImage(
						"http://lopngoaingu.com/listening/streamline-english/Directions/img/"
								+ i + "b.jpeg", i, "ESB4","b");
				LoadImage(
						"http://lopngoaingu.com/listening/streamline-english/Directions/img/"
								+ i + "c.jpeg", i, "ESC4","c");*/
				LoadImage(
						"http://lopngoaingu.com/listening/streamline-english/Directions/audio/"
								+ i + ".mp3", i, "ESS4","");
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			pDialog.dismiss();
			super.onPostExecute(result);
		}

		boolean DoDelete(File path) {
			if (path.isDirectory()) {
				for (File file : path.listFiles()) {
					DoDelete(file);
				}
			}
			return path.delete();
		}

		private void LoadImage(String mUrl, int i, String storage,String extension) {
			try {
				URL url = new URL(mUrl);
				HttpURLConnection conn = (HttpURLConnection) url
						.openConnection();
				conn.connect();
				if (conn.getResponseCode() != 404) {
					File file = new File("/data/data/com.example.lopngoaingu/"
							+ storage + "/" + i + extension+".mp3");
					FileOutputStream outputstream = new FileOutputStream(file);
					int read = 0;
					byte[] data = new byte[1024];
					InputStream input = conn.getInputStream();
					while ((read = input.read(data)) != -1) {
						outputstream.write(data, 0, read);
					}
					input.close();
					outputstream.close();
					conn.disconnect();
				}

			} catch (Exception e) {
				Log.e("Error code " + i, e.getMessage());
			}
		}
	}

}
